<template>
    <OAMain title="首页">
        <el-card>
            <template #header>
                <h2>部门员工数量</h2>
            </template>
            <div id="departmentStaffCount" style="width: 100%;height:200px;"></div>
        </el-card>
        <el-row :gutter="20">
            <el-col :span="12">
                <el-card>
                    <template #header>
                        <h2>
                            最新通知
                        </h2>
                       
                    </template>
                    <el-table :data="informs">
                            <el-table-column label="标题">
                                <template #default="scope">
                                    <router-link
                                        :to="{ name: 'informdetail', params: { pk: scope.row.id } }">{{ scope.row.title }}</router-link>
                                </template>
                            </el-table-column>
                            <el-table-column label="发布人" prop="author.realname"></el-table-column>
                            <el-table-column label="发布时间">
                                <template #default="scope">
                                    {{ dateformat.stringFromDate(scope.row.create_time) }}

                                </template>
                            </el-table-column>
                            <el-table-column>
                                <template #default="scope">
                                    <el-tag v-if="scope.row.reads.length > 0">已读</el-tag>
                                    <el-tag v-else type="danger">未读</el-tag>
                                </template>
                            </el-table-column>
                        </el-table>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card>
                    <template #header>
                        <h2>最新请假</h2>
                    </template>
                    <el-table :data="absents">
                        <el-table-column label="部门" prop="requester.department.name"></el-table-column>
                        <el-table-column label="发起人" prop="requester.realname"></el-table-column>
                        <el-table-column label="起始日期" prop="start_date"></el-table-column>
                        <el-table-column label="结束日期" prop="end_date"></el-table-column>
                        <el-table-column label="发起时间">
                            <template #default="scope">
                                {{ dateformat.stringFromDate(scope.row.create_time) }}
                            </template>
                        </el-table-column>

                    </el-table>
                </el-card>
            </el-col>
        </el-row>
    </OAMain>
</template>

<script setup name="informDetail">
import { ref, reactive, onMounted, getCurrentWatcher } from 'vue'
import OAMain from '@/components/OAMain.vue';
import OAPagination from '@/components/OAPagination.vue';
import OADialog from '@/components/OADialog.vue';
import { useAuthStore } from '@/stores/auth';
import informHttp from '@/api/informHttp';
import { ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';
import dateformat from '@/utils/dateformat';
import homeHttp from '@/api/homeHttp';
import * as echarts from 'echarts';
let informs = ref([])
let absents = ref([])

onMounted(async () => {
    try {
        let inform = await informHttp.getInformList()
        informs.value = inform.results
        absents.value = await homeHttp.getLatestAbsents()
        console.log("Absents:", absents.value)

        let rows = await homeHttp.getDepartmentStaffCount()
        let xdatas = []
        let ydatas = []
        for (let row of rows) {
            xdatas.push(row.name)
            ydatas.push(row.staff_count)
        }
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('departmentStaffCount'));
        // 绘制图表
        myChart.setOption({

            tooltip: {},
            xAxis: {
                data: xdatas
            },
            yAxis: {},
            series: [
                {
                    name: '部门人员统计',
                    type: 'bar',
                    data: ydatas
                }
            ]
        });
    } catch (detail) {
        ElMessage.error(detail)
    }


})
</script>

<style scoped></style>